package com.bootdo.shop.dao;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.bootdo.shop.domain.request.ShoppingCartReq;
import com.bootdo.shop.domain.response.ShoppingCartRes;


public interface ShoppingCartMapper {
	
	@Insert("insert into shopping_cart(user_id,goods_id,goods_num) "
			+ " values(#{userId},#{goodsId},#{goodsNum})")
	@Options(useGeneratedKeys=true)
	public Integer insert(ShoppingCartReq shoppingCartReq);
	
	@Delete("delete from shopping_cart where id = #{id}")
	public Integer delete(Integer id);
	
	@Update("update shopping_cart set goods_num = case when (goods_num + #{goodsNum}) <= 1 then 1 else goods_num + #{goodsNum} end "
			+ " where user_id = #{userId} and goods_id = #{goodsId}")
	public Integer updateNum(ShoppingCartReq shoppingCartReq);
	
	
	@Update("update shopping_cart set goods_num = case when (goods_num + #{goodsNum}) <= 1 then 1 else #{goodsNum} end "
			+ " where user_id = #{userId} and goods_id = #{goodsId} ")
	public Integer update(ShoppingCartReq shoppingCartReq);
	
	@Select("select sum(a.goods_num) cnt from shopping_cart a left join goods b on a.goods_id = b.id where a.user_id = #{userId} and b.is_onsale = 1 ")
	public Integer findShoppingCartNum(ShoppingCartReq shoppingCartReq);
	
	
	@Select("select a.id,a.goods_id,a.specification_id,a.goods_num,b.title,b.subject,b.thumbnail_image,b.goods_type,b.sale_price,"
			+ "b.market_price,b.is_onsale,b.amount,b.is_identity,b.is_invoice,b.is_use_coupon,"
			+ "b.general_user_price,b.platina_user_price,b.vip_user_price,b.agent_1_price,b.agent_2_price,b.agent_3_price,b.agent_4_price,b.agent_5_price,"
			+ "b.general_user_credits,b.platina_user_credits,b.vip_user_credits,b.agent_1_credits,b.agent_2_credits,b.agent_3_credits,b.agent_4_credits,b.agent_5_credits,b.is_fr "
			+ " from shopping_cart a left join goods b on a.goods_id = b.id where user_id = #{userId}"
			+ " and b.is_onsale = 1 " 
			+ " order by a.create_time desc")
	public List<ShoppingCartRes> findShoppingCartList(ShoppingCartReq shoppingCartReq);
	
	@Select("select * from shopping_cart where user_id = #{userId} and goods_id = #{goodsId}")
	public ShoppingCartReq findShoppingCartByGoodsIdAndUserId(ShoppingCartReq shoppingCartReq);
	
	@Select("select * from shopping_cart where id = #{id}")
	public ShoppingCartReq findShoppingCartById(ShoppingCartReq shoppingCartReq);

}
